<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('classroom_class', LIB_PATH);
$main = new Classroom($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$self = isset($_POST['self']) ? trim($_POST['self']) : trim($_GET['self']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;

switch ($cf_action) {
  case "index":	
	$smarty->mypage($cf_controller, $cf_action, 0);
	$self = isset($_POST['self']) ? trim($_POST['self']) : urldecode($_GET['self']);	
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode($_GET['skey2']);
	$skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode($_GET['skey3']);	
	$where = '1';
	
	if (is_numeric($self)) {
	    $where .= " and cr_status = '" . $self . "'";
	}
	if ($skey) {
	    $where .= " and cr_title like '%" . $skey . "%'";
	}	
	if ($skey2 && $skey3) {
   	$where .= " AND DATE_FORMAT(cr_create_time,'%Y-%m-%d')  BETWEEN '".$skey2."' AND '".$skey3."'";
 }
	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, "cr_id DESC", intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page . '?dpt=' . $cf_dpt .'&self=' . $self. '&skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3);
	
	//dump($data_ary);
	//echo $MyPage->get_sql();

	//dump($data_ary);
	break;
  case "add":
	if ($cf_ispost) {			
		$data = $db->getFormData($main->get_table_name(),$_POST);	
		$data['cr_create_time'] = date("Y-m-d H:i:s");
		$data['cr_creator'] = $cf_admin_info['admin_name'];						
		$result = $main->insert_data($data);				
	  if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
		my_jump('添加成功' . $more, '', $return_page, 3);
	    } else {
		my_jump('添加失败');
	    }
	} else {

	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';			
	}

	break;
  case "edit":
		if ($cf_ispost) {		
			$data = $db->getFormData($main->get_table_name(),$_POST);			
			$result = $main->update_data($data, "cr_id='$id'");			
			if ($result) {
				$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
				unset($_SESSION['pre_url']);
				my_success('修改成功', '', $return_page, 3);
			} else {
				my_error('修改失败');
			}
		} else {		 
			$smarty->mypage($cf_controller, $cf_action, 0);
			$_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
			$row = $main->get_data_row($id);
			if (!$row)
			my_jump('沒有此信息');		
		}
	break;
    case "del":
	$result = $main->del_data($id);
	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失败!');
	}

	break;

   

	
	default:
	my_jump('非法操作!');
	break;
}

$smarty->assign(array(
    'id' => $id,
    'row' => $row,
    'data_ary' => $data_ary,
    'page' => $page,
    'self' => $self,
    'skey' => $skey, 
    'skey2' => $skey2,
		'skey3' => $skey3,  
));

$smarty->mydisplay();
?>